package kr.hkit.androidcomplete03.ch28_1;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import kr.hkit.androidcomplete03.R;
import kr.hkit.androidcomplete03.ch28_1.common.Item;
import kr.hkit.androidcomplete03.ch28_1.common.MyAdapter;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

public class PullParser extends Activity {
	ListView listView;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.parser);
		listView = (ListView) findViewById(R.id.orderListView);
		new ParsingXml().execute("order.xml");
	}
	
	private class ParsingXml extends AsyncTask<String, Void, ArrayList<Item>>{
		ProgressDialog dlg;
		
		protected void onPreExecute() {
			dlg = ProgressDialog.show(PullParser.this, "Wait", "order.xml Parsing...");
		}
		@Override
		protected ArrayList<Item> doInBackground(String... params) {
			StringBuilder sb = new StringBuilder();
			String str=null;
			BufferedReader br=null;
			try {
				br = new BufferedReader(new InputStreamReader(openFileInput(params[0])));
				while((str=br.readLine()) != null){
					sb.append(str);
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {br.close();}catch(IOException e) {}
			}
			return parsingXml(sb.toString());
		}

		private ArrayList<Item> parsingXml(String xml) {
			final String TAG="XML PULL Parser";
			ArrayList<Item> arLists = new ArrayList<>();
			Item item=null;
			boolean initItem=false;
			
			try {
				XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
				XmlPullParser parser = factory.newPullParser();
				parser.setInput(new StringReader(xml));
				
				int eventType = parser.getEventType();
				
				while(eventType != XmlPullParser.END_DOCUMENT){
					switch(eventType){
					case XmlPullParser.START_DOCUMENT:
						Log.d(TAG, "START_DOCUMENT");
						break;
					case XmlPullParser.START_TAG:
						Log.d(TAG, "START_TAG");
						if (parser.getName().equalsIgnoreCase("item")){
							item = new Item();
							initItem = true;
						}
						//속성이 존재한다면 반드시 여기서 추출
						if (parser.getAttributeCount()>0){
							for(int i=0; i<parser.getAttributeCount(); i++){
								if (parser.getAttributeName(i).equalsIgnoreCase("price")){
									item.setItemPrice(Integer.parseInt(parser.getAttributeValue(i)));
								}else{
									item.setItemMaker(parser.getAttributeValue(i));
								}
							}
						}
						break;
					case XmlPullParser.TEXT://<item>이부분</item>
						Log.d(TAG, "TEXT");
						if (initItem){
							item.setItemName(parser.getText());
						}
						break;
					case XmlPullParser.END_TAG://</item>
						if (parser.getName().equalsIgnoreCase("item")){
							initItem = false;
							arLists.add(item);
						}
						Log.d(TAG, "END_TAG");
						break;
					case XmlPullParser.END_DOCUMENT:
						Log.d(TAG, "START_DOCUMENT");
						break;
					}
					eventType = parser.next();
				}
			} catch (XmlPullParserException | IOException e) {
				e.printStackTrace();
			}
			return arLists;
		}
		
		protected void onPostExecute(ArrayList<Item> result) {
			MyAdapter adapter = new MyAdapter(PullParser.this, result);
			listView.setAdapter(adapter);
			dlg.dismiss();
		}
		
	}
}
